/**
 * 掉落盒子脚本，实现盒子碰撞及回收流程
 */
export default class Box extends Laya.Script {
    /**刚体对象引用 */
    private _rig: Laya.RigidBody;

    /** @prop {name:box1,tips:"掉落容器预制体对象1",type:Prefab}*/
    box1: Laya.Prefab;
    private _box1: Laya.Sprite;

    /** @prop {name:box2,tips:"掉落容器预制体对象1",type:Prefab}*/
    box2: Laya.Prefab;
    private _box2: Laya.Sprite;

    /** @prop {name:box3,tips:"掉落容器预制体对象1",type:Prefab}*/
    box3: Laya.Prefab;
    private _box3: Laya.Sprite;

    constructor() { super(); }

    onEnable(): void {
       
    }

    onUpdate(): void {
       
    }

    creatBox(): Laya.Sprite{
        this._box1 = Laya.Pool.getItemByCreateFun("box1", this.box1.create, this.box1);

        this._box2 = Laya.Pool.getItemByCreateFun("box2", this.box2.create, this.box2);

        this._box3 = Laya.Pool.getItemByCreateFun("box3", this.box3.create, this.box3);
        return;
    }

    onTriggerEnter(other: any, self: any, contact: any): void {
        var owner: Laya.Sprite = this.owner as Laya.Sprite;
        if (other.label === "box1") {
            
                /* 创建下一等级盒子 并设置类型为刚体类型（默认为静态类型） */
                let _nextLevelBox : Laya.Sprite = Laya.Pool.getItemByCreateFun("nextLevelBox", this.nextLevelBox.create, this.nextLevelBox);
                _nextLevelBox.pos(owner.x, owner.y);
                let _rig:Laya.RigidBody;
                _rig = _nextLevelBox.getComponent(Laya.RigidBody);
                _rig.type = "dynamic";
                owner.parent.addChild(_nextLevelBox);

                owner.removeSelf();
                Laya.SoundManager.playSound("sound/destroy.wav");
                
            }
    }

    onDisable(): void {
        //盒子被移除时，回收盒子到对象池，方便下次复用，减少对象创建开销。
        Laya.Pool.recover("box", this.owner);
    }
}